home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / gateway.idb / usr / WebFace / Source / 20-NetworkServices / ftp / ftp-anonymous.frm.z / ftp-anonymous.frm
Encoding:
Text File  |  1997-07-30  |  8.0 KB  |  297 lines

  1. #!/usr/bin/perl5
  2. #
  3. # ftp-anonymous.cgi
  4. #
  5. # Copyright 1988-1996 Silicon Graphics, Inc.
  6. # All rights reserved.
  7. #
  8. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  9. # the contents of this file may not be disclosed to third parties, copied or
  10. # duplicated in any form, in whole or in part, without the prior written
  11. # permission of Silicon Graphics, Inc.
  12. #
  13. # RESTRICTED RIGHTS LEGEND:
  14. # Use, duplication or disclosure by the Government is subject to restrictions
  15. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  16. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  17. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  18. # rights reserved under the Copyright Laws of the United States.
  19. #
  20. # $Id: ftp-anonymous.frm,v 1.40 1997/06/24 22:20:39 shotes Exp $
  21.  
  22. # flow chart on form validation:
  23. #
  24. # if (ftp user does not already exist) 
  25. #   if (home directory already exists) -> error
  26. #   else -> create directory and ftp user
  27. # else
  28. #   if (home directory exists and owned by ftp) -> check for nec. files
  29. #   elsif (home dir. exists and not owned by ftp) -> error
  30. #   else -> create home dir. and add nec. files
  31.  
  32. BEGIN { require "/usr/WebFace/lib/CGI.pm"; import CGI; }
  33. require "/usr/OnRamp/lib/OnRamp.pm";
  34. require "/usr/OnRamp/lib/java.pm";
  35.  
  36. $query = new CGI;
  37.  
  38. $passwdfile = "/etc/passwd";
  39. $confI = "/etc/inetd.conf";
  40. $dummy = "/etc/passwd.tmp";
  41. $title = "Anonymous FTP Configuration";
  42.  
  43. if ($ENV{'HTTP_USER_AGENT'} =~ /Mozilla\/2/) { $br_index = 1; }
  44. else { $br_index = 0; }
  45.  
  46. $js = 
  47. "br_index = $br_index;
  48. $js_standard
  49. $js_error_box
  50. $js_filename
  51. function checkForm(form) {
  52.     if (form.eftp[br_index].checked) {
  53.         if (!testFilename(form.home, \"home directory name\")) return (false);
  54.     }
  55.     return (true);
  56. }";
  57.  
  58.  
  59. print $query->header;
  60. &js_title_block($title,$js);
  61.  
  62. $help = $document_root . $ENV{"SCRIPT_NAME"};
  63. $help =~ s/cgi$/hlp/;
  64. exec $help if ($query->param('help') eq "Help");
  65.  
  66. # if anonymous FTP user exists, we assume directory tree
  67. # also exists.
  68.  
  69. @getpass = getpwnam("ftp");
  70. if ($query->param('doit') eq 'Ok') {
  71.     if ($getpass[0]) {
  72.         $oldHome = $getpass[7];
  73.         $oldEftp = 'Yes';
  74.     }
  75.     if ($query->param('eftp') eq 'Yes') {
  76.         $home = $query->param('home');
  77.         &error("Invalid home directory.") if !$home;
  78.         &formValidation;
  79.         &checkInetdEntry;
  80.     
  81.         if (!$getpass[0] || $oldHome ne $home) { 
  82.             if ($getpass[0]) { &deleteOld; }
  83.             &add_password("ftp","*",500,25,"anon ftp",$home,"/bin/false"); 
  84.         }
  85.         if (-d $home) { &checkForFiles; }
  86.         else { &createHome; }
  87.         $message = "Anonymous FTP enabled.";
  88.     } else { &disable_ftp; }
  89. } elsif ($getpass[0]) {
  90.     $home = $getpass[7];
  91.     $eftp = 'Yes';
  92. } else {
  93.     $home = '/var/ftp';
  94.     $eftp = 'No';
  95. }
  96.  
  97. &generic;
  98.  
  99. print $query->end_html;
  100.  
  101. sub deleteOld {
  102.     open(IN,"< $passwdfile");
  103.     open(OUT,"> $dummy");
  104.     while(<IN>) {
  105.         @items = split(/:/);
  106.         if ($items[0] ne "ftp") { print OUT $_; }
  107.     }
  108.     close(IN);
  109.     close(OUT);
  110.     rename($dummy,$passwdfile);
  111. }        
  112.  
  113. sub checkInetdEntry {
  114.     $found = 0;
  115.     $doRename = 0;
  116.     open(IN,"< $confI");
  117.     open(OUT,"> $dummy");
  118.     while(<IN>) {
  119.         @items = split(/\s+/);
  120.         if (($items[0] eq "#" && $items[1] eq "ftp") || $items[0] eq "#ftp") {
  121.             $_ =~ /^\s*#\s*(.*)$/;
  122.             $line = $1;
  123.             print OUT "$line\n";
  124.             $found = 1;
  125.             $doRename = 1;
  126.         } elsif ($items[0] eq "ftp") {
  127.             print OUT $_;
  128.             $found = 1;
  129.         } else { print OUT $_; }
  130.     }
  131.     if (!$found) {
  132.         $doRename = 1;
  133.         print OUT "ftp\tstream\ttcp\tnowait\troot\t/user/etc/ftpd\tftpd\n";
  134.     }
  135.     close(IN);
  136.     close(OUT);
  137.     if ($doRename) { 
  138.         rename($dummy,$confI); 
  139.     system("/etc/killall", "-HUP", "inetd");
  140.     }
  141. }
  142.  
  143. sub formValidation {
  144.     if ($getpass[0]) {        # ftp user exists
  145.         if (-d $query->param('home')) {
  146.             my($dev,$ino,$mode,$nlink,$fuid,
  147.               $fgid,$rdev,$size,$atime,$mtime,
  148.               $ctime,$blksize,$blocks) = stat(_);
  149.  
  150.             my($name,$psswd,$uuid,$ugid,$quota,
  151.               $comment,$gcos,$dir,$shell) = getpwnam("ftp");
  152.  
  153.             &error("Home directory not owned by FTP") 
  154.               if $fuid != $uuid;
  155.         } elsif (-f $query->param('home')) {
  156.             &error("$home exists, but is not a directory.");
  157.         }
  158.     } else {                  #ftp user does not exist
  159.         if (-d $query->param('home')) {
  160.             &error("Home directory owned by other user.");
  161.         } elsif (-f $query->param('home')) {
  162.             &error("$home exists, but is not a directory.");
  163.         }
  164.     }
  165. }
  166.  
  167. sub error {
  168.     &error_block($_[0]);
  169.     &generic;
  170.     exit 0;
  171.  
  172. sub get_os_num {
  173.     open(IN, "/sbin/uname -r |");
  174.     $ret = <IN>; chop $ret;
  175.     close(IN);
  176.     $ret;
  177. }
  178.  
  179. sub createHome {
  180.     if (!$home) { return 0; }
  181.     # create home directory
  182.     system("/sbin/mkdir", "-p", $home);
  183.     system("/sbin/chown", "ftp.ftp", $home);
  184.  
  185.     $os_num = &get_os_num;
  186.  
  187.     # create subdirectories
  188.     mkdir("$home/bin", 0111);
  189.     mkdir("$home/pub", 0555);
  190.     mkdir("$home/etc", 0111);
  191.     mkdir("$home/lib", 0555);
  192.     mkdir("$home/dev", 0555);
  193.  
  194.     mkdir("$home/lib32", 0555) if ($os_num eq "6.4" || $os_num eq "6.5");
  195.  
  196.     system("/sbin/chown", "ftp.ftp", "$home/pub");
  197.   
  198.     system("/sbin/cp", "/bin/ls", "$home/bin"); 
  199.     chmod 0555, "$home/bin/ls";
  200.  
  201.     system("/sbin/cp", "/lib/rld", "$home/lib");
  202.     system("/sbin/cp", "/lib/libc.so.1", "$home/lib");
  203.     chmod 0555, "$home/lib/rld", "$home/lib/libc.so.1";
  204.  
  205.     if ($os_num eq "6.4" || $os_num eq "6.5") {
  206.     system("/sbin/cp", "/lib32/rld", "$home/lib32");
  207.     system("/sbin/cp", "/lib32/libc.so.1", "$home/lib32");
  208.     mkdir("$home/lib/iconv", 0555);
  209.     system("/sbin/cp", "/usr/lib/iconv/iconvtab", "$home/lib/iconv");
  210.     chmod 0555, "$home/lib/iconv/iconvtab";
  211.     }
  212.  
  213.     system("/sbin/mknod", "$home/dev/zero", "c", "37", "0");
  214. #   `mknod $home/dev/zero c 37 0`;
  215.     chmod 0444, "$home/dev/zero";
  216.  
  217.     $ftp_pass = $home . "/etc/passwd";
  218.     $ftp_grp = $home . "/etc/group";
  219.  
  220.     open(OUTP,"> $ftp_pass");
  221.     open(OUTG,"> $ftp_grp");
  222.  
  223.     my($name,$psswd,$uuid,$ugid,$quota,
  224.         $comment,$gcos,$dir,$shell) = getpwnam("ftp");
  225.     print OUTP "ftp:*:${uuid}:${ugid}:anon ftp:${dir}:${shell}\n";
  226.     print OUTG "ftp:*:${ugid}:\n";
  227.  
  228.     my($name,$psswd,$uuid,$ugid,$quota,
  229.         $comment,$gcos,$dir,$shell) = getpwnam("user");
  230.     print OUTG "user:*:${ugid}:\n";    
  231.  
  232.     my($name,$psswd,$uuid,$ugid,$quota,
  233.         $comment,$gcos,$dir,$shell) = getpwnam("root");
  234.     print OUTP "root:*:${uuid}:${ugid}::/:/bin/false\n";
  235.     print OUTG "sys:*:$ugid:\n";
  236.  
  237.     close(OUTP); 
  238.     close(OUTG);  
  239. }
  240.    
  241. sub checkForFiles { }  
  242.  
  243. sub disable_ftp {
  244.     $value = "";
  245.     &putpass;
  246.     $message = "Anonymous FTP disabled.";
  247. }
  248.  
  249. sub generic {
  250.     &header_block("Anonymous FTP");
  251.  
  252.     print "<i>$message</i>";
  253.  
  254.     print "<form name=\"StandardForm\" method=post onSubmit=\"return runSubmit()\">";
  255.  
  256.     print "<center><table cellpadding=5 width=450>\n";
  257.  
  258.     print "<tr><th align=left>Enable anonymous FTP:</th><th align=left>\n",
  259.       $query->radio_group(-name=>'eftp',
  260.       -values=>['Yes','No'], -default=>$eftp),
  261.       "</th></tr>\n";
  262.  
  263.     print "<tr><th align=left>Home directory for the FTP account:</th>\n",
  264.       "<th align=left>",
  265.       $query->textfield('home', $home),
  266.       "</th></tr>\n";
  267.  
  268.     print "</table></center><br>\n";
  269.  
  270.     print &js_buttons('doit','Ok','onClick="markOK()"','onClick="markOther()"');
  271.  
  272.     print $query->endform;
  273.  
  274.     print $query->end_html;
  275. }
  276.  
  277. sub putpass {
  278.     $variable = "ftp";
  279.     local($len) = length($variable);
  280.  
  281.     open(IN,"< $passwdfile") || print "unable to read $passwdfile";
  282.     open(OUT,"> $dummy") || print "unable to write to $dummy";
  283.     $found = 0;
  284.  
  285.     while (<IN>) {
  286.         if ($variable eq substr($_,0,$len)) {
  287.             $found = 1;
  288.             print OUT $value;
  289.         } else { print OUT $_; }
  290.     }
  291.     if ($found == 0) { print OUT $value; }
  292.     close(OUT);
  293.     close(IN);
  294.     rename($dummy, $passwdfile) || print "unable to rename $dummy";
  295. }
  296.